package com.simple.portal.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.portal.bo.IGroupInfoBO;
import com.simple.portal.bo.IOrganizationBO;
import com.simple.portal.vo.GroupInfoVO;
import com.simple.portal.vo.OrganizationVO;
import com.simple.util.TbResourceLoad;
import com.simple.util.common.Constants;
import com.simple.util.common.RightsConstants;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.portal.GroupInfoBean;
import com.simple.util.object.portal.SessionUserBean;

import freemarker.template.TemplateException;

@Controller
@RequestMapping("/group")
public class GroupController extends AbstractPortalBaseController{
	private final static String JSP_PATH = "_Group";
	

	@Autowired
	private IGroupInfoBO groupInfoBO;
	
	@Autowired
	private IOrganizationBO organizationBO;
	
	@RequestMapping("/system/list")  
    public String systemList(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    						 @RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
    						 @RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
    						 @RequestParam(value="statusSearch",required=false) String statusSearch,
					    	final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean flag = checkRights( request,response,RightsConstants.FUNC_GROUP_STSTEM_LIST);
		if(flag){
				groupInoList(pageNo, groupNameSearch, organizationIdSearch, statusSearch, request, response);
			    
			    request.setAttribute("type", "system");
		}
			    
        return getJspPath("groupinfo_list");
    }
	
	@RequestMapping("/system/add")  
    public String systemAdd(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		 checkRights( request,response,RightsConstants.FUNC_GROUP_STSTEM_ADD);
		 request.setAttribute("type", "system");
        return getJspPath("groupinfo_add");
    }
	
	@RequestMapping(value="/system/addSubmit",method=RequestMethod.POST)  
    public String systemAddSubmit(@ModelAttribute("groupInfoVO") GroupInfoVO groupInfoVO,
    							 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		
		boolean flag = checkRights( request,response,RightsConstants.FUNC_GROUP_STSTEM_ADD);
		
		if(flag){
				SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
				groupInfoBO.addGroupInfo(groupInfoVO,sessionUserBean);
		}
        return  "forward:/group/system/list" ;
    }
	
	@RequestMapping("/system/edit")  
    public String systemEdit(@RequestParam(value="pageNo",required=false) Integer pageNo,
    						 @RequestParam(value="groupId",required=false) String groupId,
    						 @RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
    						 @RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
    						 @RequestParam(value="statusSearch",required=false) String statusSearch,
    						 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_GROUP_STSTEM_EDIT);
		boolean flag = checkManagerUserGroup(groupId, request,response);

		if(rightsFlag && flag){
				request.setAttribute("groupInfoVO", groupInfoBO.findById(groupId)); 
			    request.setAttribute("statusOne", Constants.STATE_NORMAL);
			    request.setAttribute("statusZero", Constants.STATE_STOP);
			   
			    
			    request.setAttribute("pageNo", pageNo); 
			    request.setAttribute("groupNameSearch", groupNameSearch);  
			    request.setAttribute("organizationIdSearch", organizationIdSearch);  
			    request.setAttribute("statusSearch", statusSearch); 
			    request.setAttribute("type", "system");
		}
		        
        return getJspPath("groupinfo_edit");
    }
	
	@RequestMapping(value="/system/editSubmit",method=RequestMethod.POST)  
    public String systemEditSubmit(	@RequestParam(value="pageNo",required=false) Integer pageNo,
    								@ModelAttribute("groupInfoVO") GroupInfoVO groupInfoVO,
    								@RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
    	    						@RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
    	    						@RequestParam(value="statusSearch",required=false) String statusSearch,
    								final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_GROUP_STSTEM_EDIT);
		boolean flag = checkManagerUserGroup(groupInfoVO.getGroupId(), request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			groupInfoBO.editGroupInfo(groupInfoVO, sessionUserBean);
		}
        
        return  "forward:/group/system/list";
    }
	
	
	@RequestMapping("/system/control")  
    public String systemControl(@RequestParam(value="pageNo",required=false) Integer pageNo,
    							@RequestParam(value="groupId",required=false) String groupId,
    							@RequestParam(value="status",required=false) String status,
								@RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
								@RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
								@RequestParam(value="statusSearch",required=false) String statusSearch,
    							final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag= checkRights( request,response,RightsConstants.FUNC_GROUP_STSTEM_CONTROL);
		boolean flag = checkManagerUserGroup(groupId, request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			
			groupInfoBO.controlGroupInfo(groupId, status, sessionUserBean);
		}
	    
	    return "forward:/group/system/list";
       
    }
	
	@RequestMapping("/enterprise/list")  
    public String enterpriseList(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
								 @RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
								 @RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
								 @RequestParam(value="statusSearch",required=false) String statusSearch,
    							 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		boolean flag = checkRights( request,response,RightsConstants.FUNC_GROUP_ENTERPRISE_LIST);
		if(flag){
			
				groupInoList(pageNo, groupNameSearch, organizationIdSearch, statusSearch, request, response);
			    
			    request.setAttribute("type", "enterprise");
		}

        
        return getJspPath("groupinfo_list");
    }
	
	@RequestMapping("/enterprise/add")  
    public String enterpriseAdd(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		 checkRights( request,response,RightsConstants.FUNC_GROUP_ENTERPRISE_ADD);
		 request.setAttribute("type", "enterprise");
        return getJspPath("groupinfo_add");
        
    }
	
	@RequestMapping(value="/enterprise/addSubmit",method=RequestMethod.POST)  
    public String enterpriseAddSubmit(@ModelAttribute("groupInfoVO") GroupInfoVO groupInfoVO,
    							 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		boolean flag = checkRights( request,response,RightsConstants.FUNC_GROUP_ENTERPRISE_ADD);
		
		if(flag){
				SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
				groupInfoBO.addGroupInfo(groupInfoVO,sessionUserBean);
		}
        return  "forward:/group/enterprise/list" ;
    }
	
	@RequestMapping("/enterprise/edit")  
    public String enterpriseEdit(@RequestParam(value="pageNo",required=false) Integer pageNo,
	    						 @RequestParam(value="groupId",required=false) String groupId,
	    						 @RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
	    						 @RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
	    						 @RequestParam(value="statusSearch",required=false) String statusSearch,
	    						 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_GROUP_ENTERPRISE_EDIT);
		boolean flag = checkManagerUserGroup(groupId, request,response);

		if(rightsFlag && flag){
				request.setAttribute("groupInfoVO", groupInfoBO.findById(groupId)); 
			    request.setAttribute("statusOne", Constants.STATE_NORMAL);
			    request.setAttribute("statusZero", Constants.STATE_STOP);
			   
			    
			    request.setAttribute("pageNo", pageNo); 
			    request.setAttribute("groupNameSearch", groupNameSearch);  
			    request.setAttribute("organizationIdSearch", organizationIdSearch);  
			    request.setAttribute("statusSearch", statusSearch); 
			    request.setAttribute("type", "enterprise");
		}
		        
        return getJspPath("groupinfo_edit");
    }
	
	@RequestMapping(value="/enterprise/editSubmit",method=RequestMethod.POST)  
    public String enterpriseEditSubmit(	@RequestParam(value="pageNo",required=false) Integer pageNo,
	    								@ModelAttribute("groupInfoVO") GroupInfoVO groupInfoVO,
	    								@RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
	    	    						@RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
	    	    						@RequestParam(value="statusSearch",required=false) String statusSearch,
	    								final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_GROUP_ENTERPRISE_EDIT);
		boolean flag = checkManagerUserGroup(groupInfoVO.getGroupId(), request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			groupInfoBO.editGroupInfo(groupInfoVO, sessionUserBean);
		}
        
        return "forward:/group/enterprise/list";
    }
	
	
	@RequestMapping("/enterprise/control")  
    public String enterpriseControl(@RequestParam(value="pageNo",required=false) Integer pageNo,
	    							@RequestParam(value="groupId",required=false) String groupId,
	    							@RequestParam(value="status",required=false) String status,
									@RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
									@RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
									@RequestParam(value="statusSearch",required=false) String statusSearch,
	    							final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag= checkRights( request,response,RightsConstants.FUNC_GROUP_ENTERPRISE_CONTROL);
		boolean flag = checkManagerUserGroup(groupId, request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			
			groupInfoBO.controlGroupInfo(groupId, status, sessionUserBean);
		}
	    
	    return "forward:/group/enterprise/list";
       
    }
	
	@RequestMapping("/partner/list")  
    public String partnerList(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
							  @RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
							  @RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
							  @RequestParam(value="statusSearch",required=false) String statusSearch,
    						  final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		boolean flag = checkRights( request,response,RightsConstants.FUNC_GROUP_PARTNTER_LIST);
		if(flag){
			
				groupInoList(pageNo, groupNameSearch, organizationIdSearch, statusSearch, request, response);
			    
			    request.setAttribute("type", "partner");
		}

        
        return getJspPath("groupinfo_list");
    }
	
	@RequestMapping("/partner/add")  
    public String partnerAdd(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		 checkRights( request,response,RightsConstants.FUNC_GROUP_PARTNTER_ADD);
		 request.setAttribute("type", "partner");
        return getJspPath("groupinfo_add");
        
    }
	
	@RequestMapping(value="/partner/addSubmit",method=RequestMethod.POST)  
    public String partnerAddSubmit(@ModelAttribute("groupInfoVO") GroupInfoVO groupInfoVO,
    							 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		boolean flag = checkRights( request,response,RightsConstants.FUNC_GROUP_PARTNTER_ADD);
		
		if(flag){
				SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
				groupInfoBO.addGroupInfo(groupInfoVO,sessionUserBean);
		}
        return  "forward:/group/partner/list" ;
    }
	
	@RequestMapping("/partner/edit")  
    public String partnerEdit(@RequestParam(value="pageNo",required=false) Integer pageNo,
    						  @RequestParam(value="groupId",required=false) String groupId,
    						  @RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
    						  @RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
    						  @RequestParam(value="statusSearch",required=false) String statusSearch,
    						  final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_GROUP_PARTNTER_EDIT);
		boolean flag = checkManagerUserGroup(groupId, request,response);

		if(rightsFlag && flag){
				request.setAttribute("groupInfoVO", groupInfoBO.findById(groupId)); 

			    request.setAttribute("statusOne", Constants.STATE_NORMAL);
			    request.setAttribute("statusZero", Constants.STATE_STOP);
			   
			    
			    request.setAttribute("pageNo", pageNo); 
			    request.setAttribute("groupNameSearch", groupNameSearch);  
			    request.setAttribute("organizationIdSearch", organizationIdSearch);  
			    request.setAttribute("statusSearch", statusSearch); 
			    request.setAttribute("type", "partner");
		}
        return getJspPath("groupinfo_edit");
    }
	
	@RequestMapping(value="/partner/editSubmit",method=RequestMethod.POST)  
    public String partnerEditSubmit(@RequestParam(value="pageNo",required=false) Integer pageNo,
	    							@ModelAttribute("groupInfoVO") GroupInfoVO groupInfoVO,
	    							@RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
	    	    					@RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
	    	    					@RequestParam(value="statusSearch",required=false) String statusSearch,
	    							final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_GROUP_PARTNTER_EDIT);
		boolean flag = checkManagerUserGroup(groupInfoVO.getGroupId(), request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			groupInfoBO.editGroupInfo(groupInfoVO, sessionUserBean);
		}
        
        return "forward:/group/partner/list";
    }
	
	@RequestMapping("/partner/control")  
    public String partnerControl(@RequestParam(value="pageNo",required=false) Integer pageNo,
	    							@RequestParam(value="groupId",required=false) String groupId,
	    							@RequestParam(value="status",required=false) String status,
									@RequestParam(value="groupNameSearch",required=false) String groupNameSearch,
									@RequestParam(value="organizationIdSearch",required=false) String organizationIdSearch,
									@RequestParam(value="statusSearch",required=false) String statusSearch,
	    							final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag= checkRights( request,response,RightsConstants.FUNC_GROUP_PARTNTER_CONTROL);
		boolean flag = checkManagerUserGroup(groupId, request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			
			groupInfoBO.controlGroupInfo(groupId, status, sessionUserBean);
		}
	    
	    return "forward:/group/partner/list";
       
    }
	
	private void groupInoList(final Integer pageNo,String groupNameSearch,final  String organizationIdSearch,final String statusSearch,final HttpServletRequest request, final HttpServletResponse response){
		
		final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
        SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
        List<GroupInfoBean> groupInfoBeanList = groupInfoBO.findGroupInfoByPage(sessionUserBean.getUserInfoVO().getUserId(), groupNameSearch, organizationIdSearch, statusSearch, pageInfoBean);
        
        List<OrganizationVO> organizationVOList = organizationBO.findOrganizationVOList(sessionUserBean.getUserInfoVO().getUserId());
        
		Map<String, String> adminFlagMap= TbResourceLoad.getInstance().getMap(Constants.TBRESOURCES_PACKAGE_MAP_BOOLEAN, getSiteLanguage(request));
		
		Map<String, String> statusMap= TbResourceLoad.getInstance().getMap(Constants.TBRESOURCES_PACKAGE_MAP_STATUS, getSiteLanguage(request));
		
        request.setAttribute("groupInfoBeanList", groupInfoBeanList);
        request.setAttribute("organizationVOList", organizationVOList);
        request.setAttribute("adminFlagMap", adminFlagMap);
        request.setAttribute("statusMap", statusMap);
		 
		final String pageInfo = super.generatePageInfo(request,response,pageInfoBean, "goPage");
	    request.setAttribute("pageInfo", pageInfo);  
	    
	    request.setAttribute("pageNo", pageNo); 
	    request.setAttribute("groupNameSearch", groupNameSearch);  
	    request.setAttribute("organizationIdSearch", organizationIdSearch);  
	    request.setAttribute("statusSearch", statusSearch); 
	    
	    request.setAttribute("statusOne", Constants.STATE_NORMAL);
	    request.setAttribute("statusZero", Constants.STATE_STOP);
	    request.setAttribute("recordCountInOnePage", PageInfoBean.COMMON_RECORD_COUNT_IN_ONE_PAGE);
	    request.setAttribute("siteLanguage", getSiteLanguage(request));
	}
	
	@Override
    protected String getBaseJspPath() {
        return JSP_PATH;
    }
}

